import numpy as np
import matplotlib.pyplot as plt

secswinedge_values = [81.21, 21.28, 86.72, 67.08, 84.65, 80.01, 94.89, 63.48, 91.48, 81.47]

# 所有模型的数据用于计算置信区间
all_models_data = {
    'DSC': [76.85, 77.77, 80.39, 78.59, 77.48, 79.13, 79.88, 81.21],
    'HD': [39.70, 36.02, 14.70, 26.59, 31.69, 21.55, 27.98, 21.28],
    'Aorta': [89.07, 89.55, 86.21, 87.92, 87.23, 85.47, 86.63, 86.72],
    'Gallbladder': [69.72, 68.88, 65.69, 64.99, 63.13, 66.53, 61.51, 67.08],
    'Kidney (L)': [77.77, 77.98, 85.23, 81.47, 81.87, 83.28, 83.74, 84.65],
    'Kidney (R)': [68.60, 71.11, 79.77, 77.29, 77.02, 79.61, 78.39, 80.01],
    'Liver': [93.43, 93.57, 94.61, 93.06, 94.08, 94.29, 95.02, 94.89],
    'Pancreas': [53.98, 58.04, 59.52, 59.46, 55.86, 56.58, 64.06, 63.48],
    'Spleen': [86.67, 87.30, 90.99, 87.75, 85.08, 90.66, 89.19, 91.48],
    'Stomach': [75.58, 75.75, 81.08, 76.81, 75.62, 76.60, 80.48, 81.47]
}

organs = ['DSC', 'HD', 'Aorta', 'Gallbladder', 'Kidney (L)', 'Kidney (R)',
          'Liver', 'Pancreas', 'Spleen', 'Stomach']

# 计算每个器官的95%置信区间
errors = []
for organ in organs:
    organ_data = all_models_data[organ]
    mean = np.mean(organ_data)
    std = np.std(organ_data, ddof=1)  # ddof=1 for sample standard deviation
    n = len(organ_data)

    # 使用t分布而不是正态分布（更适合小样本）
    from scipy import stats

    t_value = stats.t.ppf(0.975, df=n - 1)  # 0.975 for 95% CI (two-tailed)
    confidence_interval = t_value * std / np.sqrt(n)
    errors.append(confidence_interval)

# 创建图形
fig = plt.figure(figsize=(10, 6))

# 设置Times New Roman字体
plt.rcParams['font.family'] = 'Times New Roman'
# 设置不同的颜色
colors = ['purple', 'darkviolet', 'navy', 'steelblue', 'teal', 'turquoise',
          'mediumseagreen', 'yellowgreen', 'gold', 'yellow']

# 绘制柱状图和误差条
bars = plt.bar(range(len(organs)), secswinedge_values, yerr=errors,
               error_kw={'capsize': 5, 'elinewidth': 1, 'capthick': 1},
               color=colors, alpha=0.8, label='Mean +95% CI',
               width=0.45)

# 设置标签
plt.xticks(range(len(organs)), organs, rotation=45, ha='right')
plt.ylabel('Value')
# plt.title('CSWin-UNet Performance with 95% Confidence Intervals')

# 添加图例
plt.legend()

# 调整布局
plt.tight_layout()

# 显示图形
plt.show()

fig.tight_layout()
fig.savefig(r'test.pdf', bbox_inches='tight')